package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.Personnel;
import com.tongzhan.service.PersonnelService;
import com.tongzhan.utils.RegexUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/personnel")
public class PersonnelController {

    @Resource
    private PersonnelService personnelService;

    /**
     * 新增入驻人员接口
     *
     * @param personnelCode 新增用户编码（新增时不传此参数）
     * @param userId        用户id
     * @param name          姓名
     * @param age           年龄
     * @param sex           性别
     * @param duties        身份
     * @param address       住址
     * @param telephone     电话
     * @param education     文化程度
     * @param isOutsiders   是否暂住 0-否 常驻 1-是 暂住
     * @param cause         来访事由
     * @param source        来源地
     * @param isGreenCode   是否绿码
     * @param inTime        入驻时间
     * @param frontPhoto    来访人正面照图片编码
     * @param certificates  身份证正反面图片
     * @param residences    居住证正反面图片
     * @param teachingStaff 教职人员证图片编码
     * @return Return
     */
    @PostMapping("/newPersonnel")
    public Return newPersonnel(String personnelCode, String userId, String name, Integer age, Integer sex, String duties, String address,
                               String telephone, String education, Integer isOutsiders, String cause,
                               String source, Integer isGreenCode, String inTime, String frontPhoto, String certificates,
                               String residences, String teachingStaff) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(name) || StringUtils.isEmpty(name.trim()) || age == null || sex == null
                || StringUtils.isEmpty(address) || StringUtils.isEmpty(address.trim()) || StringUtils.isEmpty(telephone)
                || isOutsiders == null || StringUtils.isEmpty(inTime) || StringUtils.isEmpty(frontPhoto)
                || StringUtils.isEmpty(certificates)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (name.trim().length() > 20) {
            return new Return(Return.FAIL, "姓名过长");
        }
        if (address.trim().length() > 500) {
            return new Return(Return.FAIL, "地址过长");
        }
        //校验手机号码
        if (!RegexUtil.validPhoneNum("0", telephone)) {
            return new Return(Return.FAIL, "手机格式不正确");
        }
        if (StringUtils.isNotEmpty(duties) && duties.trim().length() > 100) {
            return new Return(Return.FAIL, "身份过长");
        }
        if (StringUtils.isNotEmpty(education) && education.trim().length() > 100) {
            return new Return(Return.FAIL, "文化程度过长");
        }
        if (StringUtils.isNotEmpty(cause) && cause.trim().length() > 1000) {
            return new Return(Return.FAIL, "来访缘由过长");
        }
        if (StringUtils.isNotEmpty(source) && source.trim().length() > 200) {
            return new Return(Return.FAIL, "来源地过长");
        }
        Personnel personnel = new Personnel();
        personnel.setPersonnelCode(personnelCode);
        personnel.setRealName(name.trim());
        personnel.setSex(sex);
        personnel.setAge(age);
        personnel.setTelephone(telephone);
        personnel.setAddress(address.trim());
        personnel.setCause(cause);
        if (StringUtils.isNotEmpty(source)) {
            personnel.setSource(source.trim());
        }
        personnel.setIsGreenCode(isGreenCode);
        if (StringUtils.isNotEmpty(duties)) {
            personnel.setDuties(duties.trim());
        }
        if (StringUtils.isNotEmpty(education)) {
            personnel.setEducation(education.trim());
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        try {
            personnel.setInTime(LocalDate.parse(inTime, dtf));
        } catch (Exception e) {
            return new Return(Return.FAIL, "入职时间格式不正确");
        }
        personnel.setFrontPhoto(frontPhoto);
        personnel.setCertificates(certificates);
        personnel.setResidences(residences);
        personnel.setTeachingStaff(teachingStaff);
        if (isOutsiders == 0) {
            personnel.setType(1);
        } else {
            personnel.setType(0);
        }
        return personnelService.newPersonnel(userId, personnel);
    }

    /**
     * 获取人员信息列表接口
     *
     * @param userId       用户id
     * @param religionCode 宗教编码
     * @param siteCode     场所编码
     * @param type         类型
     * @param page         页
     * @param size         大小
     * @return Return
     */
    @PostMapping("/getPersonList")
    public Return getPersonList(String userId, String religionCode, String siteCode, Integer type, Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId) || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return personnelService.getPersonList(userId, siteCode, type, page, size, religionCode);
    }

    /**
     * 人员详细信息接口
     *
     * @param personnelCode 人员信息编码
     * @return Return
     */
    @PostMapping("/personDetails")
    public Return personDetails(String personnelCode) {
        if (StringUtils.isEmpty(personnelCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return personnelService.personDetails(personnelCode);
    }

    /**
     * 暂住人员离开接口
     *
     * @param personnelCode 人员信息编码
     * @return Return
     */
    @PostMapping("/leave")
    public Return leave(String personnelCode) {
        if (StringUtils.isEmpty(personnelCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return personnelService.leave(personnelCode);
    }

    /**
     * 人员详细信息回显（照片区分）
     *
     * @param personnelCode 人员信息编码
     * @return Return
     */
    @PostMapping("/returnPersonDetails")
    public Return returnPersonDetails(String personnelCode) {
        if (StringUtils.isEmpty(personnelCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return personnelService.returnPersonDetails(personnelCode);
    }

    /**
     * 人员详细信息回显（照片区分）
     *
     * @param personnelCode 人员信息编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String personnelCode) {
        if (StringUtils.isEmpty(personnelCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return personnelService.delete(personnelCode);
    }
}
